package com.example.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ruiji.pojo.R;
import com.example.ruiji.pojo.User;
import com.example.ruiji.service.UserService;
import com.example.ruiji.utils.MailRequest;
import com.example.ruiji.utils.SendMailService;
import com.example.ruiji.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RequestMapping("/user")
@Slf4j
@RestController
@Component
public class UserController {
@Autowired
private UserService userService;

    @Autowired
    private SendMailService sendMailService;
///发送短信
    @PostMapping("/sendMsg")
    public R<String> send(@RequestBody User user, HttpSession session)
    {

        //接收手机号
        String phone = user.getPhone();

        //生成四位验证码
        if(phone!=null)
        {
            String code= ValidateCodeUtils.generateValidateCode(4).toString();
        //生成的验证码保存在seiion中 下次继续判断 同时发送邮箱
      //验证码发送
            MailRequest mailRequest=new MailRequest();
            mailRequest.setSendTo(phone);
            mailRequest.setSubject("小橘外卖验证码");
            mailRequest.setText("你的四位验证码为"+code+"四分钟后失效");

            sendMailService.sendSimpleMail(mailRequest);
            log.info(code);
            session.setAttribute(phone,code);

            return R.success("发送短信成功");
        }

        return R.success("发送短信失败");
    }
    //写登录 发验证码 判断验证码 --最后实现是由邮箱发送 前后端的代码都需要改动
@PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session)
//请求以map的方式传入键值对中
     {
         //拿出验证码和电话
       String phone= (String) map.get("phone");
         String coide= (String) map.get("code");

         //拿出之前保存的seeion
         String attribute = session.getAttribute(phone).toString();

         //判断是否登录成功
         if(attribute.equals(coide)&& attribute!=null)
         {
             //登录成功  第一保存seeion 第2判断是否为新用户需不需要入库
             LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
             queryWrapper.eq(User::getPhone,phone);
             User user=userService.getOne(queryWrapper);
             if(user==null)
             {
                 user=new User();
                 user.setPhone(phone);
                 user.setStatus(1);
                 userService.save(user);
             }
             session.setAttribute("user",user.getId());
             return R.success(user);


         }
         return R.error("登录失败");
     }

    @Scheduled(fixedRate = 10000)
    public void dx( String phone,HttpSession session)
    {
        if (session.getAttribute("phone") != null) {
            session.removeAttribute("phone");
            return;
        }
        return;
    }
    @PostMapping("/loginout")
    public R<String> delet(HttpSession session)
    {
        //清楚当前的seeion

        session.invalidate();
        return R.success("成功");
    }

}
